package com.l4.software.pixie.kernel;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Area;
import java.awt.image.PixelGrabber;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import com.sun.awt.AWTUtilities;


/**
 * DeskPixie主窗口
 * @author 李骞
 *
 */
public class Main extends JFrame implements Floatable {
 
	private static final long serialVersionUID = 1L;
	
	//TODO:Main相关组件设计,如下：
	private JPopupMenu popupMenu;	 
	private FPManager fpManager;	 
	private MainControl mainControl; 
	private SkillSet skillSet;	 
	private Pixie pixie;
	private Launcher launcher;
	
	private Point origin;
	private static Image backimg;

	//TODO:通过properties文件读入用户组件并计算组件数量
	private int module_amout =2;  //用户组件数量
	
	
	static{
		backimg=GraphicsUtils.loadImage("remi.png");
		System.setProperty("sun.java2d.noddraw", "true");
	}
	
	public Main(){
		super();
		launcher=null;
	}
	
	public Main(Launcher launcin){
		super();
		launcher=launcin;
	}
	
	//初始化程序,由Launcher调用
	public synchronized void initialize() {
		setInitState("初始化Kernel...");
		initKernel();		
		setInitState("初始化Module...");
		initModule();
	}
	 
	private synchronized void setInitState(String state) {
		if(launcher!=null){
			launcher.setState(state);
		}		
	}

	private void initKernel() {
		//初始化Main
		this.setSize(534, 372);
		this.setTitle("DeskPixie");
		this.setUndecorated(true);
		
		AWTUtilities.setWindowShape(this,getImageShape(backimg, true));
		
		this.origin=new Point();
				
		AWTUtilities.setWindowOpacity(this, 0.8f);
		
		this.setLocationRelativeTo(null);
		
		
		addMouseListener( 
				new MouseAdapter(){
					public void mousePressed(MouseEvent e){
						origin.x = e.getX();
						origin.y = e.getY();
					}
					public void mouseClicked(MouseEvent e) {
						if(e.getButton()==MouseEvent.BUTTON3)
							System.exit(0);
					}
					public void mouseReleased(MouseEvent e) {
						super.mouseReleased(e);
					}
					public void mouseEntered(MouseEvent e) {
						repaint();							
					}						
				}
		);


		addMouseMotionListener(
				new MouseMotionAdapter(){
					public void mouseDragged(MouseEvent e){
						Point p =  getLocation();
						setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y );
					}
					
				}
		);
	}
	
	
	protected void initModule() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(backimg, 0, 0, null);
	}
	
	
	
	
	public Shape getImageShape(Image img, boolean isFiltrate) {
		ArrayList<Integer> x=new ArrayList<Integer>();
		ArrayList<Integer> y=new ArrayList<Integer>();		
		int width=img.getWidth(null);
		int height=img.getHeight(null);

		// 透明化处理
		PixelGrabber pgr = new PixelGrabber(img, 0, 0, -1, -1, true);
		try {
			pgr.grabPixels();
		} catch (InterruptedException ex) {
			ex.getStackTrace();
		}
		int pixels[] = (int[]) pgr.getPixels();
		if (isFiltrate) {
			// 循环像素
			for (int i = 0; i < pixels.length; i++) {
				//透明化
				DColor color = DColor.getLColor(pixels[i]);
				if (color.A == 0){
					continue;
				//}else if((i-1<0 ? true:(LColor.getLColor(pixels[i-1]).A!=0)) 
				//		&& (i+1>pixels.length ? true:LColor.getLColor(pixels[i+1]).A!=0)
				//		&& (i-width<0 ? true:LColor.getLColor(pixels[i-width]).A!=0) 
				//		&& (i+width>pixels.length ? true:LColor.getLColor(pixels[i+width]).A!=0)){
				//	continue;
				}else{
					x.add(i%width>0 ? i%width-1:0);
					y.add(i%width==0 ? (i==0 ? 0:i/width-1):i/width);
				}
			}
		}
		
		int[][] matrix=new int[height][width];
		
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				matrix[i][j]=0;
			}
		}
		
		for(int c=0;c<x.size();c++){
			matrix[y.get(c)][x.get(c)]=1;
		}
		
		Area rec=new Area();
		int temp=0;

		
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				if(matrix[i][j]==1){
					if(temp==0)
						temp=j;	
					else if(j==width){
						if(temp==0){
							Rectangle rectemp=new Rectangle(j,i,1,1);
							rec.add(new Area(rectemp));
						}else{
							Rectangle rectemp=new Rectangle(temp,i,j-temp,1);
							rec.add(new Area(rectemp));
							temp=0;
						}
					}
				}else{
					if(temp!=0){
						Rectangle rectemp=new Rectangle(temp,i,j-temp,1);
						rec.add(new Area(rectemp));
						temp=0;
					}
				}
			}
			temp=0;
		}

		return rec;
	}
	 
	public void open(){
		this.setVisible(true);
	}
	
	/**
	 * 获取用户组件数量
	 * @return 用户组件数量
	 */
	public int getModuleAmout() {		
		return module_amout;
	}
}
 
